{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:49:59.878439Z",
     "iopub.status.busy": "2024-11-29T09:49:59.878439Z",
     "iopub.status.idle": "2024-11-29T09:50:00.655403Z",
     "shell.execute_reply": "2024-11-29T09:50:00.655403Z",
     "shell.execute_reply.started": "2024-11-29T09:49:59.878439Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 层级索引\n",
    "## 介绍\n",
    "下面创建一个Series， 在输入索引Index时，输入了由两个子list组成的list，第一个子list是外层索引，第二个list是内层索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.656658Z",
     "iopub.status.busy": "2024-11-29T09:50:00.656658Z",
     "iopub.status.idle": "2024-11-29T09:50:00.671264Z",
     "shell.execute_reply": "2024-11-29T09:50:00.671264Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.656658Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a  0   -0.630500\n",
      "   1   -3.726570\n",
      "   2    0.487199\n",
      "b  0    0.155872\n",
      "   1    0.326666\n",
      "   2   -0.899732\n",
      "c  0    0.372880\n",
      "   1    0.070857\n",
      "   2   -0.131243\n",
      "d  0    0.834696\n",
      "   1    1.017617\n",
      "   2   -0.337951\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "ser_obj = pd.Series(np.random.randn(12),index=[\n",
    "                ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'],\n",
    "                [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]\n",
    "            ])\n",
    "print(ser_obj)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "MultiIndex索引对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.672546Z",
     "iopub.status.busy": "2024-11-29T09:50:00.672546Z",
     "iopub.status.idle": "2024-11-29T09:50:00.688005Z",
     "shell.execute_reply": "2024-11-29T09:50:00.686778Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.672546Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.indexes.multi.MultiIndex'>\n"
     ]
    }
   ],
   "source": [
    "print(type(ser_obj.index))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.688774Z",
     "iopub.status.busy": "2024-11-29T09:50:00.688774Z",
     "iopub.status.idle": "2024-11-29T09:50:00.702258Z",
     "shell.execute_reply": "2024-11-29T09:50:00.702258Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.688774Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MultiIndex([('a', 0),\n",
      "            ('a', 1),\n",
      "            ('a', 2),\n",
      "            ('b', 0),\n",
      "            ('b', 1),\n",
      "            ('b', 2),\n",
      "            ('c', 0),\n",
      "            ('c', 1),\n",
      "            ('c', 2),\n",
      "            ('d', 0),\n",
      "            ('d', 1),\n",
      "            ('d', 2)],\n",
      "           )\n"
     ]
    }
   ],
   "source": [
    "print(ser_obj.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.703433Z",
     "iopub.status.busy": "2024-11-29T09:50:00.703433Z",
     "iopub.status.idle": "2024-11-29T09:50:00.718850Z",
     "shell.execute_reply": "2024-11-29T09:50:00.717738Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.703433Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['a', 'b', 'c', 'd'], [0, 1, 2]]\n"
     ]
    }
   ],
   "source": [
    "print(ser_obj.index.levels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "## 选取\n",
    "● 根据索引获取数据。因为现在有两层索引，当通过外层索引获取数据的时候，可以直接利用外层索引的标签来获取。\n",
    "● 当要通过内层索引获取数据的时候，在list中传入两个元素，前者是表示要选取的外层索引，后者表示要选取的内层索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.720049Z",
     "iopub.status.busy": "2024-11-29T09:50:00.720049Z",
     "iopub.status.idle": "2024-11-29T09:50:00.733930Z",
     "shell.execute_reply": "2024-11-29T09:50:00.733053Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.720049Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.155872\n",
       "1    0.326666\n",
       "2   -0.899732\n",
       "dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1.外层选取\n",
    "ser_obj['b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.735382Z",
     "iopub.status.busy": "2024-11-29T09:50:00.733930Z",
     "iopub.status.idle": "2024-11-29T09:50:00.749555Z",
     "shell.execute_reply": "2024-11-29T09:50:00.748328Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.735382Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    0.487199\n",
       "b   -0.899732\n",
       "c   -0.131243\n",
       "d   -0.337951\n",
       "dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2.内层获取  常用于分组操作、透视表的生成等\n",
    "ser_obj[:,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.750558Z",
     "iopub.status.busy": "2024-11-29T09:50:00.749555Z",
     "iopub.status.idle": "2024-11-29T09:50:00.764618Z",
     "shell.execute_reply": "2024-11-29T09:50:00.763615Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.750558Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(-0.6304997014665701)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ser_obj['a',0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "## 交换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.765756Z",
     "iopub.status.busy": "2024-11-29T09:50:00.765756Z",
     "iopub.status.idle": "2024-11-29T09:50:00.780796Z",
     "shell.execute_reply": "2024-11-29T09:50:00.779522Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.765756Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0  a   -0.630500\n",
       "1  a   -3.726570\n",
       "2  a    0.487199\n",
       "0  b    0.155872\n",
       "1  b    0.326666\n",
       "2  b   -0.899732\n",
       "0  c    0.372880\n",
       "1  c    0.070857\n",
       "2  c   -0.131243\n",
       "0  d    0.834696\n",
       "1  d    1.017617\n",
       "2  d   -0.337951\n",
       "dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1.swaplevel（）交换内层和外层的索引\n",
    "ser_obj.swaplevel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.782097Z",
     "iopub.status.busy": "2024-11-29T09:50:00.780796Z",
     "iopub.status.idle": "2024-11-29T09:50:00.795566Z",
     "shell.execute_reply": "2024-11-29T09:50:00.794437Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.782097Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a  0   -0.630500\n",
       "   1   -3.726570\n",
       "   2    0.487199\n",
       "b  0    0.155872\n",
       "   1    0.326666\n",
       "   2   -0.899732\n",
       "c  0    0.372880\n",
       "   1    0.070857\n",
       "   2   -0.131243\n",
       "d  0    0.834696\n",
       "   1    1.017617\n",
       "   2   -0.337951\n",
       "dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2.sort_index()先对外层索引进行排序，在对内层索引进行排序， 默认升序\n",
    "ser_obj.sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.795566Z",
     "iopub.status.busy": "2024-11-29T09:50:00.795566Z",
     "iopub.status.idle": "2024-11-29T09:50:00.810002Z",
     "shell.execute_reply": "2024-11-29T09:50:00.810002Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.795566Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0  a   -0.630500\n",
       "   b    0.155872\n",
       "   c    0.372880\n",
       "   d    0.834696\n",
       "1  a   -3.726570\n",
       "   b    0.326666\n",
       "   c    0.070857\n",
       "   d    1.017617\n",
       "2  a    0.487199\n",
       "   b   -0.899732\n",
       "   c   -0.131243\n",
       "   d   -0.337951\n",
       "dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#交换并排序分层\n",
    "ser_obj.swaplevel().sort_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 统计计算和描述"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "|方法\t| 说明                          |\n",
    "|---|-----------------------------|\n",
    "|count\t| 非NA值的数量                     |\n",
    "|describe| \t针对Series或各DataFrame列计算汇总统计 |\n",
    "|min、max| \t计算最小值和最大值                  |\n",
    "|argmin、argmax\t| 计算能够获取到最小值和最大值的索引位置（整数)     |\n",
    "|idxmin、idxmax\t| 计算能够获取到最小值和最大值的索引值          |\n",
    "|quantile\t| 计算样本的分位数（0到1)               |\n",
    "|sum\t| 值的总和                        |\n",
    "|mean\t| 值的平均数                       |\n",
    "|median\t| 值的算术中位数(50%分位数)             |\n",
    "|mad\t| 根据平均值计算平均绝对离差               |\n",
    "|var\t| 样本值的方差                      |\n",
    "|std\t| 样本值的标准差                     |\n",
    "|skew\t| 样本值的偏度（三阶矩)                 |\n",
    "|kurt\t| 样本值的峰度（四阶矩)                 |\n",
    "|cumsum\t| 样本值的累计和                     |\n",
    "|cummin、cummax| \t样本值的累计最大值和累计最小值            |\n",
    "|cumprod\t| 样本值的累计积                     |\n",
    "|diff\t| 计算一阶差分（对时间序列很有用)            |\n",
    "|pct_change| \t计算百分数变化                    |\n",
    "\n",
    "1. axis=0 按列统计，axis=1按行统计\n",
    "2. skipna 排除缺失值， 默认为True\n",
    "3. level  如果轴是层次化索引的（即Multilndex)，则根据level分组约简"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 简单统计指标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.811156Z",
     "iopub.status.busy": "2024-11-29T09:50:00.811156Z",
     "iopub.status.idle": "2024-11-29T09:50:00.825429Z",
     "shell.execute_reply": "2024-11-29T09:50:00.825429Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.811156Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1.40</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>7.10</td>\n",
       "      <td>-4.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>0.75</td>\n",
       "      <td>-1.3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    one  two\n",
       "a  1.40  NaN\n",
       "b  7.10 -4.5\n",
       "c   NaN  NaN\n",
       "d  0.75 -1.3"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=pd.DataFrame([[1.4,np.nan],[7.1,-4.5],\n",
    "                [np.nan,np.nan],[0.75,-1.3]],\n",
    "                index=['a','b','c','d'],\n",
    "                columns=['one','two'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.826834Z",
     "iopub.status.busy": "2024-11-29T09:50:00.826834Z",
     "iopub.status.idle": "2024-11-29T09:50:00.841100Z",
     "shell.execute_reply": "2024-11-29T09:50:00.841100Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.826834Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one    9.25\n",
       "two   -5.80\n",
       "dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#默认按列求和\n",
    "df.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.842178Z",
     "iopub.status.busy": "2024-11-29T09:50:00.842178Z",
     "iopub.status.idle": "2024-11-29T09:50:00.856916Z",
     "shell.execute_reply": "2024-11-29T09:50:00.856412Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.842178Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a     NaN\n",
       "b    2.60\n",
       "c     NaN\n",
       "d   -0.55\n",
       "dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按行求和\n",
    "df.sum(axis=1,skipna=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.857930Z",
     "iopub.status.busy": "2024-11-29T09:50:00.857930Z",
     "iopub.status.idle": "2024-11-29T09:50:00.872462Z",
     "shell.execute_reply": "2024-11-29T09:50:00.871455Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.857930Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one    b\n",
       "two    d\n",
       "dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看每一列所有行的最大值所在的标签索引，\n",
    "# 同样我们也可以通过axis='columns'求每一行所有列的最大值的标签索引\n",
    "df.idxmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.873481Z",
     "iopub.status.busy": "2024-11-29T09:50:00.872462Z",
     "iopub.status.idle": "2024-11-29T09:50:00.886760Z",
     "shell.execute_reply": "2024-11-29T09:50:00.886760Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.873481Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1.40</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>8.50</td>\n",
       "      <td>-4.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>9.25</td>\n",
       "      <td>-5.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    one  two\n",
       "a  1.40  NaN\n",
       "b  8.50 -4.5\n",
       "c   NaN  NaN\n",
       "d  9.25 -5.8"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.cumsum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.887970Z",
     "iopub.status.busy": "2024-11-29T09:50:00.887970Z",
     "iopub.status.idle": "2024-11-29T09:50:00.902870Z",
     "shell.execute_reply": "2024-11-29T09:50:00.901596Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.887970Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>3.083333</td>\n",
       "      <td>-2.900000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>3.493685</td>\n",
       "      <td>2.262742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.750000</td>\n",
       "      <td>-4.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.075000</td>\n",
       "      <td>-3.700000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.400000</td>\n",
       "      <td>-2.900000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>4.250000</td>\n",
       "      <td>-2.100000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>7.100000</td>\n",
       "      <td>-1.300000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            one       two\n",
       "count  3.000000  2.000000\n",
       "mean   3.083333 -2.900000\n",
       "std    3.493685  2.262742\n",
       "min    0.750000 -4.500000\n",
       "25%    1.075000 -3.700000\n",
       "50%    1.400000 -2.900000\n",
       "75%    4.250000 -2.100000\n",
       "max    7.100000 -1.300000"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 汇总统计\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:20.471926Z",
     "iopub.status.busy": "2024-11-29T09:50:20.471926Z",
     "iopub.status.idle": "2024-11-29T09:50:20.487347Z",
     "shell.execute_reply": "2024-11-29T09:50:20.486843Z",
     "shell.execute_reply.started": "2024-11-29T09:50:20.471926Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0.00</th>\n",
       "      <td>0.750</td>\n",
       "      <td>-4.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.25</th>\n",
       "      <td>1.075</td>\n",
       "      <td>-3.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.50</th>\n",
       "      <td>1.400</td>\n",
       "      <td>-2.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.75</th>\n",
       "      <td>4.250</td>\n",
       "      <td>-2.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.00</th>\n",
       "      <td>7.100</td>\n",
       "      <td>-1.3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        one  two\n",
       "0.00  0.750 -4.5\n",
       "0.25  1.075 -3.7\n",
       "0.50  1.400 -2.9\n",
       "0.75  4.250 -2.1\n",
       "1.00  7.100 -1.3"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.quantile(q=[0, 0.25, 0.5, 0.75, 1])  # 百分位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2024-11-29T09:50:00.903901Z",
     "iopub.status.busy": "2024-11-29T09:50:00.902870Z",
     "iopub.status.idle": "2024-11-29T09:50:00.918156Z",
     "shell.execute_reply": "2024-11-29T09:50:00.917112Z",
     "shell.execute_reply.started": "2024-11-29T09:50:00.903901Z"
    },
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     16\n",
       "unique     3\n",
       "top        a\n",
       "freq       8\n",
       "dtype: object"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = pd.Series(['a','a','b','c']*4)\n",
    "s1.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引标签与位置获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:51:54.832034Z",
     "iopub.status.busy": "2024-11-29T09:51:54.831032Z",
     "iopub.status.idle": "2024-11-29T09:51:54.848213Z",
     "shell.execute_reply": "2024-11-29T09:51:54.847212Z",
     "shell.execute_reply.started": "2024-11-29T09:51:54.832034Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(1)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"one\"].argmax()  # 返回最大值索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:52:11.160360Z",
     "iopub.status.busy": "2024-11-29T09:52:11.160360Z",
     "iopub.status.idle": "2024-11-29T09:52:11.180361Z",
     "shell.execute_reply": "2024-11-29T09:52:11.180361Z",
     "shell.execute_reply.started": "2024-11-29T09:52:11.160360Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(3)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"one\"].argmin()  # 最小值索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:52:12.760445Z",
     "iopub.status.busy": "2024-11-29T09:52:12.760445Z",
     "iopub.status.idle": "2024-11-29T09:52:12.775429Z",
     "shell.execute_reply": "2024-11-29T09:52:12.775429Z",
     "shell.execute_reply.started": "2024-11-29T09:52:12.760445Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one    b\n",
       "two    d\n",
       "dtype: object"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.idxmax()  # 返回最大值的标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:52:22.129586Z",
     "iopub.status.busy": "2024-11-29T09:52:22.129586Z",
     "iopub.status.idle": "2024-11-29T09:52:22.151804Z",
     "shell.execute_reply": "2024-11-29T09:52:22.150801Z",
     "shell.execute_reply.started": "2024-11-29T09:52:22.129586Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one    d\n",
       "two    b\n",
       "dtype: object"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.idxmin()  # 返回最小值标签"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 更多统计指标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:53:09.176928Z",
     "iopub.status.busy": "2024-11-29T09:53:09.176928Z",
     "iopub.status.idle": "2024-11-29T09:53:09.186953Z",
     "shell.execute_reply": "2024-11-29T09:53:09.186953Z",
     "shell.execute_reply.started": "2024-11-29T09:53:09.176928Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1.40</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>8.50</td>\n",
       "      <td>-4.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>9.25</td>\n",
       "      <td>-5.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    one  two\n",
       "a  1.40  NaN\n",
       "b  8.50 -4.5\n",
       "c   NaN  NaN\n",
       "d  9.25 -5.8"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.cumsum()  # 累加和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:55:22.645965Z",
     "iopub.status.busy": "2024-11-29T09:55:22.645965Z",
     "iopub.status.idle": "2024-11-29T09:55:22.667873Z",
     "shell.execute_reply": "2024-11-29T09:55:22.667873Z",
     "shell.execute_reply.started": "2024-11-29T09:55:22.645965Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1.400</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>9.940</td>\n",
       "      <td>-4.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>7.455</td>\n",
       "      <td>5.85</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     one   two\n",
       "a  1.400   NaN\n",
       "b  9.940 -4.50\n",
       "c    NaN   NaN\n",
       "d  7.455  5.85"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.cumprod()  # 累乘和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:55:24.424876Z",
     "iopub.status.busy": "2024-11-29T09:55:24.423854Z",
     "iopub.status.idle": "2024-11-29T09:55:24.442475Z",
     "shell.execute_reply": "2024-11-29T09:55:24.441273Z",
     "shell.execute_reply.started": "2024-11-29T09:55:24.424876Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one    3.493685\n",
       "two    2.262742\n",
       "dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.std()  # 标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:55:29.769229Z",
     "iopub.status.busy": "2024-11-29T09:55:29.769229Z",
     "iopub.status.idle": "2024-11-29T09:55:29.783642Z",
     "shell.execute_reply": "2024-11-29T09:55:29.782435Z",
     "shell.execute_reply.started": "2024-11-29T09:55:29.769229Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one    12.205833\n",
       "two     5.120000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.var()  # ⽅差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:55:34.681454Z",
     "iopub.status.busy": "2024-11-29T09:55:34.681454Z",
     "iopub.status.idle": "2024-11-29T09:55:34.701575Z",
     "shell.execute_reply": "2024-11-29T09:55:34.700249Z",
     "shell.execute_reply.started": "2024-11-29T09:55:34.681454Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>5.7</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   one  two\n",
       "a  NaN  NaN\n",
       "b  5.7  NaN\n",
       "c  NaN  NaN\n",
       "d  NaN  NaN"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.diff()  # 差分，当前数据减去上一个的差值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:55:44.616896Z",
     "iopub.status.busy": "2024-11-29T09:55:44.616896Z",
     "iopub.status.idle": "2024-11-29T09:55:44.641656Z",
     "shell.execute_reply": "2024-11-29T09:55:44.640378Z",
     "shell.execute_reply.started": "2024-11-29T09:55:44.616896Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\98614\\AppData\\Local\\Temp\\ipykernel_6924\\1453865397.py:1: FutureWarning: The default fill_method='pad' in DataFrame.pct_change is deprecated and will be removed in a future version. Either fill in any non-leading NA values prior to calling pct_change or specify 'fill_method=None' to not fill NA values.\n",
      "  df.pct_change().round(3)  # 计算百分比变化\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4.071</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>d</th>\n",
       "      <td>-0.894</td>\n",
       "      <td>-0.711</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     one    two\n",
       "a    NaN    NaN\n",
       "b  4.071    NaN\n",
       "c  0.000  0.000\n",
       "d -0.894 -0.711"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pct_change().round(3)  # 计算百分比变化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 高级统计指标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:56:03.154259Z",
     "iopub.status.busy": "2024-11-29T09:56:03.153255Z",
     "iopub.status.idle": "2024-11-29T09:56:03.166887Z",
     "shell.execute_reply": "2024-11-29T09:56:03.165884Z",
     "shell.execute_reply.started": "2024-11-29T09:56:03.154259Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>12.205833</td>\n",
       "      <td>-10.16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-10.160000</td>\n",
       "      <td>5.12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           one    two\n",
       "one  12.205833 -10.16\n",
       "two -10.160000   5.12"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.cov()  # 协方差：自己和别人计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:56:22.415510Z",
     "iopub.status.busy": "2024-11-29T09:56:22.415510Z",
     "iopub.status.idle": "2024-11-29T09:56:22.429776Z",
     "shell.execute_reply": "2024-11-29T09:56:22.429776Z",
     "shell.execute_reply.started": "2024-11-29T09:56:22.415510Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(-10.16)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"one\"].cov(df[\"two\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:56:37.496121Z",
     "iopub.status.busy": "2024-11-29T09:56:37.496121Z",
     "iopub.status.idle": "2024-11-29T09:56:37.508949Z",
     "shell.execute_reply": "2024-11-29T09:56:37.507946Z",
     "shell.execute_reply.started": "2024-11-29T09:56:37.496121Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>1.0</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>-1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     one  two\n",
       "one  1.0 -1.0\n",
       "two -1.0  1.0"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.corr()  # 相关性系数 -1 ~ 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-11-29T09:56:46.130313Z",
     "iopub.status.busy": "2024-11-29T09:56:46.129310Z",
     "iopub.status.idle": "2024-11-29T09:56:46.140599Z",
     "shell.execute_reply": "2024-11-29T09:56:46.140599Z",
     "shell.execute_reply.started": "2024-11-29T09:56:46.130313Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "one   -1.0\n",
       "two    1.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.corrwith(df[\"two\"])  # 一列的相关性系数"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
